package Client;


import SQL.Connections;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
import java.util.Vector;

//购票
public class Ticket extends JFrame{
    private JPanel panel1;
    private JRadioButton 一等座RadioButton;
    private JRadioButton 二等座RadioButton;
    private JButton 确认Button;
    private JFormattedTextField nameTextField;
    private JFormattedTextField phoneTextField;
    private JFormattedTextField idTextField;
    private JButton 返回Button;
    private JLabel ticketTitle;
    public static JFrame f;
    private static String classno;
    private Statement stm;

    public Ticket() {
        返回Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //跳转
                f.dispose();
                PassengerHome passengerHome=new PassengerHome();
                PassengerHome.initialize(classno);
            }
        });
        确认Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String TIDCardNumber=idTextField.getText();
                String name=nameTextField.getText();
                String phone=phoneTextField.getText();
                Random r = new Random();
                int seat = r.nextInt(100 + 1);
                int ticket=r.nextInt(1000000 + 1);
                if(!(TIDCardNumber.isEmpty()&&name.isEmpty()&&phone.isEmpty())){

                    if(一等座RadioButton.isEnabled()){
                        try {
                            String sql=("insert into ticket values('"+ticket+"','" + classno + "','"+seat+ "','"+TIDCardNumber+"','630')");
                            Connections connections = new Connections();
                            Connection con = connections.getConnection();
                            stm = con.createStatement();
                            int re = stm.executeUpdate(sql);
                        } catch (SQLException throwables) {
                            throwables.printStackTrace();
                        }


                    }else if(二等座RadioButton.isEnabled()){
                        try {
                            String sql=("insert into ticket values('"+ticket+"','" + classno + "','"+seat+ "','"+TIDCardNumber+"','300')");
                            Connections connections = new Connections();
                            Connection con = connections.getConnection();
                            stm = con.createStatement();
                            int re = stm.executeUpdate(sql);

                        } catch (SQLException throwables) {
                            throwables.printStackTrace();
                        }
                    }

                    f.dispose();
                    PassengerHome passengerHome=new PassengerHome();
                    PassengerHome.initialize();
                    JOptionPane.showMessageDialog(panel1,"购票成功","提示",JOptionPane.WARNING_MESSAGE);
                }else {
                    JOptionPane.showMessageDialog(panel1,"请输入");
                }


            }
        });
    }
    public static void initialize() {
        initialize(classno);

    }

    public static void main(String[] args) {
        initialize();


    }

    public static void initialize(String no) {
        JFrame frame = new JFrame("Ticket");
        frame.setContentPane(new Ticket().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);//设置屏幕居中
        frame.setVisible(true);
        f=frame;
        classno=no;

    }

    private void createUIComponents() {
        // TODO: place custom component creation code here
        ticketTitle = new JLabel( new ImageIcon("ticket.png"));
    }
}
